import 'package:flutter/material.dart';
import 'hm_navigator.dart';

mixin CHPageLifecycleMixin<T extends StatefulWidget> on State<T>
    implements RouteAware {
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    HMNavigator.routeObserver
        .subscribe(this, ModalRoute.of(context) as PageRoute);
  }

  @override
  void dispose() {
    HMNavigator.routeObserver.unsubscribe(this);
    super.dispose();
  }

  @override
  void didPush() {
    onPageEnter();
  }

  @override
  void didPop() {
    onPageLeave();
  }

  @override
  void didPopNext() {
    onPageEnter();
  }

  @override
  void didPushNext() {
    onPageLeave();
  }

  /// 页面进入时调用
  void onPageEnter() {}

  /// 页面离开时调用
  void onPageLeave() {}
}
